package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.*;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    private String uploadFileName;

    @RequestMapping("findByPage")
    public JsonResult findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "2")Integer pageSize){

            System.out.println("findByPage");
            //设置分页参数
            PageHelper.startPage(pageNum,pageSize);
            //查询所有用户
            List<UserTable> users = userService.findAll();
            //封装到PageInfo对象中
            PageInfo<UserTable> pageInfo = new PageInfo<>(users);
            //返回JsonResult对象
            JsonResult jsonResult = JsonResult.ok();
            jsonResult.setCount(pageInfo.getTotal());
            jsonResult.setData(pageInfo.getList());

            return jsonResult;
    }

    @RequestMapping("deleteAll")
    public String deleteAll(Integer [] ids){
        //System.out.println(Arrays.toString(ids));
        userService.deleteAll(ids);
        return "success";
    }

    @RequestMapping("excel")
    public String excel(Integer [] ids){
        //调用service层导出到Excel方法
        return userService.excel(ids) == null ? "false" : "success";
    }

    @RequestMapping("updateUser")
    public String updateUser(UserTable userTable){
        System.out.println(userTable);
        userService.updateUser(userTable);
        return "success";
    }

    @RequestMapping("deleteUserById")
    public String deleteUserById(Integer id){
        userService.deleteUserById(id);
        return "success";
    }


    @RequestMapping("upload")
    public JsonResult upload(MultipartFile file){
        //上传的原文件名
        uploadFileName = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName = UUID.randomUUID().toString().replace("-","")+uploadFileName;
        //声明上传的路径
        String uploadPath = "E:\\StudyDATA\\apache-tomcat-8.5.31\\webapps\\guimei";

        try {
            //上传
            file.transferTo(new File(uploadPath,uploadFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8088/guimei/"+uploadFileName);

        return jsonResult;
    }

    @RequestMapping("addUser")
    public String addUser(UserTable userTable){
        //设置用户图片地址
        userTable.setUserImage("http://localhost:8088/guimei/"+uploadFileName);
        System.out.println(userTable);

        userService.addUser(userTable);

        return "success";
    }

    @RequestMapping("testList")
    public JsonResult testList(Integer id){
        TestBean testBean = new TestBean(1001, "张三", "男", "上海",
                "个性签名", "748949",99, "上班", "1亿");
        TestBean testBean1 = new TestBean(1002, "张三", "男", "上海",
                "个性签名", "748949",99, "上班", "1亿");
        TestBean testBean2 = new TestBean(1003, "张三", "男", "上海",
                "个性签名", "748949",99, "上班", "1亿");
        TestBean testBean3 = new TestBean(1004, "张三", "男", "上海",
                "个性签名", "748949",99, "上班", "1亿");
        TestBean testBean4 = new TestBean(1005, "张三", "男", "上海",
                "个性签名", "748949",99, "上班", "1亿");

        List<TestBean> testBeans = new ArrayList<>();

        if (id != null) {
            testBeans.add(testBean);
        } else {
            testBeans.add(testBean);
            testBeans.add(testBean);
            testBeans.add(testBean1);
            testBeans.add(testBean2);
            testBeans.add(testBean3);
            testBeans.add(testBean4);
        }


        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(Long.valueOf(5));
        jsonResult.setData(testBeans);


        return jsonResult;
    }

}
